package com.dunka.demo.controller;

import com.dunka.demo.pojo.Data_Dictionary;
import com.dunka.demo.service.Data_DictionaryService;
import com.dunka.demo.util.Page;
import com.dunka.demo.util.RestResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Dunka
 * @Description 数据字典控制类
 * @Date 10:31   2019/7/25
 * @ClassName Data_DictionaryController
 */
@RestController
@RequestMapping("/data_dictionary")
@Api(tags = "数据字典管理相关接口",description = "提供数据字典管理相关的RestAPI")
public class Data_DictionaryController {
    @Autowired
    private Data_DictionaryService data_dictionaryService;
    /***
    * @Description: 返回数据字典已有的所有数据，暂时以字符串返回数据
    * @Param:
    * @return: java.lang.String
    * @Author: Dunka
    * @Date: 2019/7/25 12:11
    */
    @ApiImplicitParam(name = "GMC-ACCESS-TOKEN",value = "登录时得到的token",paramType = "header")
    @ApiOperation(value = "分页获取对应的数据字典项",
            notes = "传入参数：" +
                    "@RequestParam int currIndex,@RequestParam int pageSize ,@RequestParam Integer id"+
                    "返回类型中Object 里存放的data 是 Page(pageIndex,pageSize,pageCount,pageContent)",
            response = Object.class)
    @GetMapping("getAll")
    public Object getAllData_Dictionary(@RequestParam(value = "currIndex",required = true) int currIndex,
                                        @RequestParam(value = "pageSize",required = true) int pageSize,
                                        @RequestParam(value = "id",required = false) Integer id){
        Map<String,Object> data = new HashMap<>();
        data.put("currIndex",(currIndex)*pageSize);
        data.put("pageSize",pageSize);
        if (id!=null){
            data.put("id",id);
        }
        List<Data_Dictionary> list = data_dictionaryService.getAllData_Dictionary(data);
        Integer count = data_dictionaryService.countData_Dictionary(data);

        Page page = new Page();
        page.setPageIndex(currIndex);
        page.setPageSize(pageSize);
        page.setPageCount(count);
        page.setPageContent(list);

        if (list!=null){
            return RestResponseUtil.success(page);
        }else{
            return RestResponseUtil.notFound();
        }
    }


    @ApiOperation(value = "通过数据字典id获取对应的数据字典项",
            notes = "传入参数：" +
                    "@PathVariable int id"+
                    "返回类型：" +
                    "Object 里存放的data 是data_dictionary",
            response = Object.class)
    @GetMapping("/getData_DictionaryById")
    @ApiIgnore
    public Object getData_DicitonaryById(@RequestParam(value = "id") int id){
        Data_Dictionary data_dictionary = data_dictionaryService.getData_DicitonaryById(id);
        if (data_dictionary != null){
            return RestResponseUtil.success(data_dictionary);
        }else{
            return RestResponseUtil.notFound();
        }
    }

    @ApiImplicitParam(name = "GMC-ACCESS-TOKEN",value = "登录时得到的token",paramType = "header")
    @ApiOperation(value = "增加数据字典项",
            notes = "传入参数：" +
                    "@RequestBody Data_Dictionary data_dictionary"+
                    "返回类型：" +
                    "Object 里存放的data 是boolean",
            response = Object.class)
    @PostMapping("/addData_Dictionary")
    public Object addData_Dictionary(@RequestBody Data_Dictionary data_dictionary){
        boolean flag = data_dictionaryService.addData_Dictionary(data_dictionary);
        if (flag){
            return RestResponseUtil.success();
        }else{
            return RestResponseUtil.failure("添加数据字典失败");
        }
    }

    @ApiImplicitParam(name = "GMC-ACCESS-TOKEN",value = "登录时得到的token",paramType = "header")
    @ApiOperation(value = "删除数据字典项",
            notes = "传入参数：" +
                    "@RequestParam(value = \"id\") int id"+
                    "返回类型：" +
                    "Object 里存放的data 是boolean",
            response = Object.class)
    @DeleteMapping("/deleteData_Dictionary")
    public Object deteleData_Dictionary(@RequestParam(value = "id") int id){
        boolean flag = data_dictionaryService.deleteData_Dictionary(id);
        if (flag){
            return RestResponseUtil.success();
        }else{
            return RestResponseUtil.failure("删除数据字典失败");
        }
    }

    @ApiImplicitParam(name = "GMC-ACCESS-TOKEN",value = "登录时得到的token",paramType = "header")
    @ApiOperation(value = "修改数据字典项",
            notes = "传入参数：" +
                    "@RequestBody Data_Dictionary data_dictionary"+
                    "返回类型：" +
                    "Object 里存放的data 是boolean",
            response = Object.class)
    @PostMapping("/updateData_Dictionary")
    public Object updateData_Dictionary(@RequestBody Data_Dictionary data_dictionary){
        boolean flag = data_dictionaryService.updateData_Dictionary(data_dictionary);
        if (flag){
            return RestResponseUtil.success();
        }else{
            return RestResponseUtil.failure("修改数据字典失败");
        }
    }

    /***
    * @Description:批量添加数据（测试用）
    * @Param: [list]
    * @return: void
    * @Author: Dunka
    * @Date: 2019/7/29 17:00
    */
    @ApiIgnore
    @PostMapping("/addAll")
    public void batchAddData_Dictionary(@RequestBody List<Data_Dictionary> list){
        data_dictionaryService.batchAddData_Dictionary(list);
    }
}
